package com.admin.client.components;

import java.util.List;

import com.admin.client.AppConstants;
import com.admin.client.locale.AdminConstants;
import com.admin.client.mvc.events.AppEvents;
import com.admin.share.model.MenuDTO;
import com.admin.share.services.MenuServiceAsync;
import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.data.ModelIconProvider;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.TreePanelEvent;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbstractImagePrototype;


public class MenuLateral extends VerticalPanel {

	public TreePanel<MenuDTO> tree;	
	final TreeStore<MenuDTO> storeLateral = new TreeStore<MenuDTO>();
	private String hash = Window.Location.getHash();
	
	public MenuLateral(String usuario_logueado_id){
	
		final MenuServiceAsync ms = Registry.get(AppConstants.MENU_SERVICE);
		final AdminConstants constants = Registry.get(AppConstants.CONSTANTS);		
		
		MenuDTO mn = new MenuDTO(constants.principalInicio(), "inicio");		
		Dispatcher.forwardEvent(AppEvents.TabSelected, mn);
		
		ms.getMenusPermitidos(usuario_logueado_id, new AsyncCallback<List<MenuDTO>>() {

			@Override
			public void onFailure(Throwable caught) {					
				MessageBox.alert(constants.administradorConsultoriaSTI(), constants.errorConexion(), null);				
			}

			@Override
			public void onSuccess(List<MenuDTO> mnu) {
				storeLateral.removeAll();
				storeLateral.add(mnu, true);				
				MenuDTO mn;
				if(!hash.equals("")){
					hash = hash.substring(1);
					if(!hash.equals("inicio"))
						mn = findMenu(hash);
					else
						mn = new MenuDTO(constants.principalInicio(), "inicio");		
										
					Dispatcher.forwardEvent(AppEvents.TabSelected, mn);
				}
					
			}
			
		});	
		
		
		tree = new TreePanel<MenuDTO>(storeLateral);  
	    
	    tree.setDisplayProperty("menu"); 
	    tree.setWidth(200);
		
	   
	    tree.setIconProvider(new ModelIconProvider<MenuDTO>() {  
	      public AbstractImagePrototype getIcon(MenuDTO model) {
	    	  String url = model.getParametro();
	    	  
	        if (!url.equals("")) {
	        	return IconHelper.createPath("images/icons/table.png");
	        }  
	        return null;  
	      }  
	    });
	    
	    tree.addListener(Events.OnClick, new Listener<TreePanelEvent<MenuDTO>>() {
            public void handleEvent(TreePanelEvent<MenuDTO> be) {                
            	MenuDTO item = be.getItem();
            	if(!item.getParametro().equals("")){            		
            		Registry.register(AppConstants.PERMISOS, item.permisos);
            		Dispatcher.forwardEvent(AppEvents.TabSelected, item);
            	}            	
            };
        });
	       
	    addStyleName("x-small-editor");	    
	    add(tree);
	}
	
	private MenuDTO findMenu(String parametro){
		for(MenuDTO mn : storeLateral.getModels()){
			if(parametro.equals(mn.getParametro())){
				return mn;
			}			
		}
		return null;
	}
}
